#include "configwidget_db.h"

#include <iostream>

namespace verema
{

ConfigWidget_Db::ConfigWidget_Db(QWidget *cw)
: vbl_mainlayout()
, grl_top_layout()
, hbl_button_layout()
, lbl_database_type("Datenbanktyp", this)
, lbl_database_name("Datenbankname", this)
, cmb_database_type(this)
, led_database_name(this)
, btn_save("Speichern",this)
, btn_cancel("Abbrechen",this)
, configWidget(cw)
{
	grl_top_layout.addWidget(&lbl_database_type, 0, 0);
	grl_top_layout.addWidget(&cmb_database_type, 0, 1);
	grl_top_layout.addWidget(&lbl_database_name, 1, 0);
	grl_top_layout.addWidget(&led_database_name, 1, 1);

	hbl_button_layout.addWidget(&btn_save);
	hbl_button_layout.addWidget(&btn_cancel);

	vbl_mainlayout.addLayout(&grl_top_layout);
	vbl_mainlayout.addLayout(&hbl_button_layout);

	setLayout(&vbl_mainlayout);

    cmb_database_type.addItem("Sqlite3");

	//Connections
	connect(&btn_cancel, SIGNAL(clicked()), this, SLOT(close()));
	connect(&led_database_name, SIGNAL(textChanged(QString)), this, SLOT(definitionChange()));
	connect(&btn_save, SIGNAL(clicked()), this, SLOT(saveDefinitions()));

	btn_save.setDisabled(true);
}

void ConfigWidget_Db::definitionChange()
{
	btn_save.setEnabled(true);
}


void ConfigWidget_Db::loadData()
{
	led_database_name.setText(Config::getInstance()->dbname().c_str());
	btn_save.setEnabled(false);
}


void ConfigWidget_Db::saveDefinitions()
{
	Config::getInstance()->dbname(led_database_name.text().toStdString());
	btn_save.setEnabled(false);

    emit configChanged();
}

void ConfigWidget_Db::close()
{
    configWidget->close();
}

} // namespace verema
